package com.wfs.aoawlms.controller;

import com.wfs.aoawlms.pojo.Result;
import com.wfs.aoawlms.pojo.User;
import com.wfs.aoawlms.service.impl.LoginServicelmpl;
import com.wfs.aoawlms.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@CrossOrigin
@RestController
public class LoginController {
    @Autowired
    private LoginServicelmpl loginServicelmpl;
    @PostMapping("/login")
    public Result login(@RequestBody User user){
        String username = user.getUsername();
        String password = user.getPassword();
        log.info("用户登录，账号：{}密码：{}",username,password);
        User loginUser = loginServicelmpl.login(username,password);
        if(loginUser!=null){
            Map<String,Object> claims = new HashMap<>();
            claims.put("id",loginUser.getUserId());
            claims.put("username",loginUser.getUsername());
            String token = JwtUtils.generateJwt(claims);
            log.info("生成的token:{}",token);
            return Result.success(token);
        }
        return Result.error("用户名或密码错误");
    }
}
